package com.myproject.shop.controller;


import com.myproject.shop.domain.Member;
import com.myproject.shop.service.IMemberService;
import com.myproject.shop.util.AjaxResult;
import com.myproject.shop.util.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
    @RequestMapping("/api/member")
    @Api(tags = "会员管理825王慧敏")
    public class MemberController {
        @Autowired
        private IMemberService memberService;

        @ApiOperation("获得全部会员列表825王慧敏")
        @GetMapping("/listAll")
        public AjaxResult getAllMembersList() {
            return AjaxResult.success(memberService.
                    selectAllMembers());
        }

        @ApiOperation("根据name查询会员列表825王慧敏")
        @ApiImplicitParam(name = "name", value = "会员姓名",
                dataType = "string", dataTypeClass = String.class)
        @GetMapping("/list")
        public AjaxResult list(@RequestParam(value = "name", required = false) String memberName) {
            List<Member> list = memberService.selectMembersByParam(memberName);
            return AjaxResult.success(list);
        }

        @ApiOperation("根据id获取会员详细信息825王慧敏")
        @ApiImplicitParam(name = "id", value = "会员编号（ID）",
                required = true, dataType = "integer", paramType = "path",
                dataTypeClass = Integer.class)
        @GetMapping("/{id}")
        public AjaxResult getInfo(@PathVariable("id") Integer mid) {
            Member member = memberService.selectMemberById(mid);
            if (member == null || member.getMid() == null) {
                throw new BizException(500, "会员 " + mid + " 不存在");
            }
            return AjaxResult.success(member);
        }

        @ApiOperation("新增会员825王慧敏")
        @PostMapping
        public AjaxResult add(@RequestBody Member member) {
            return AjaxResult.success(memberService.insertMember(member));
        }

        @ApiOperation("修改会员825王慧敏")
        @PutMapping
        public AjaxResult edit(@RequestBody Member member) {
            return AjaxResult.success(memberService.updateMember(member));
        }

        @ApiOperation("删除会员825王慧敏")
        @ApiImplicitParam(name = "mid", value = "会员编号（ID）", required = true,
                dataType = "integer", paramType = "path", dataTypeClass = Integer.class)
        @DeleteMapping("/{mid}")
        public AjaxResult remove(@PathVariable Integer mid) {
            return AjaxResult.success(memberService.deleteMemberById(mid));
        }
    }

